package com.ygsoft.tianlu_mall.controller;


import com.ygsoft.tianlu_mall.pojo.Msg;
import com.ygsoft.tianlu_mall.service.TraceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
public class TraceController {

    @Autowired
    TraceService traceService;

    //浏览商品立即被添加到足迹
    @RequestMapping(value = "/addGoods2Trace",method = RequestMethod.POST)
    @ResponseBody
    public Msg addGoods2Trace(@RequestParam int user_id,@RequestParam int goods_id){
        try {
            traceService.addGoods2Trace(user_id,goods_id);
            return Msg.succes();
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }
    //获得足迹信息
    @RequestMapping(value = "/getTrace",method = RequestMethod.GET)
    @ResponseBody
    public Msg getTrace(@RequestParam int user_id){
        try {
            List<HashMap<String,Object>> traceList = traceService.getTrace(user_id);
            HashMap<String,Object> result = new HashMap<>();
            result.put("traceList",traceList);
            return Msg.succes().add("result",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //删除单条足迹信息
    @RequestMapping(value = "/deleteOneTrace",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteOneTrace(@RequestParam int user_id, @RequestParam int trace_id){
        try {
            traceService.deleteOneTrace(user_id,trace_id);
            return Msg.succes();
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //按时间删除足迹信息
    @RequestMapping(value = "/deleteTraceByTime",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteTraceByTime(@RequestParam int user_id,@RequestParam @DateTimeFormat(pattern="yyyy-MM-dd") Date trace_time){
        try {
            traceService.deleteTraceByTime(user_id,trace_time);
            return Msg.succes();
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }
}
